package com.village.zpark.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.village.zpark.mapper.GovernanceMapper;
import com.village.zpark.pojo.Governance;
import com.village.zpark.pojo.Production;
import com.village.zpark.service.GovernanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class GovernanceServiceImpl implements GovernanceService {
    @Autowired
    private GovernanceMapper governanceMapper;
    @Override
    public List<Governance> findAll() {
        return governanceMapper.selectAll();
    }

    @Override
    public Governance findById(Integer id) {
        return governanceMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addGovernance(Governance governance) {
        return governanceMapper.insert(governance);
    }

    @Override
    public int updateGovernance(Governance governance) {
        return governanceMapper.updateByPrimaryKey(governance);
    }

    @Override
    public int deleteGovernanceById(Integer id) {
        return governanceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Governance> findPage(int page, int size, String param) {
        PageHelper.startPage(page, size);
        Example example = new Example(Governance.class);
        Example.Criteria criteria = example.createCriteria();
        if (param != null && !param.isEmpty()) {
            criteria.orLike("governancearea", "%" + param + "%")
                    .orLike("grade", "%" + param + "%")
                    .orLike("evaluationdetails", "%" + param + "%"); // 使用 orLike 来实现多字段模糊查询
        }
        List<Governance> governances;
        if (param != null && !param.isEmpty()) {
            governances = governanceMapper.selectByExample(example);
        } else {
            governances = governanceMapper.selectAll(); // 当搜索栏为空时，返回全部表单信息
        }
        return new PageInfo<>(governances);
    }
}
